package jmf.network;

import java.io.IOException;
import java.io.InputStream;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.PullSourceStream;
import javax.media.protocol.SourceStream;

public class NetworkRawSourceStream implements PullSourceStream {
  private boolean eof;
  private InputStream inputStream;
  private ContentDescriptor contentDescriptor;

  public NetworkRawSourceStream(InputStream input, ContentDescriptor desc) {
    inputStream = input;
    contentDescriptor = desc;
    eof = false;
  }

  public ContentDescriptor getContentDescriptor() {
    return contentDescriptor;
  }

  public boolean endOfStream() {
    return eof;
  }

  public boolean willReadBlock() {
    try {
      return eof || inputStream.available() == 0;
    } catch (IOException e) {
      e.printStackTrace();
      return true;
    }
  }

  public int read(byte[] buffer, int offset, int length) throws IOException {
    int n = inputStream.read(buffer, offset, length);

    if (n == -1)
      eof = true;

    return n;
  }

  public long getContentLength() {
    return SourceStream.LENGTH_UNKNOWN;
  }

  public Object[] getControls() {
    return new Object[0];
  }

  public Object getControl(String controlType) {
    return null;
  }
}
